/*!
 * @file Controller.cpp
 * @author Rocco Martino
 */
/***************************************************************************
 *   Copyright (C) 2014 by Rocco Martino                                   *
 *   martinorocco@gmail.com                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation; either version 2.1 of the  *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

/* ======================================================================= */
/* ....................................................................... */
#include <ooGame/Game>

#include <osgODE/Notify>
#include <osgODE/RigidBody>
/* ....................................................................... */
/* ======================================================================= */




using namespace ooGame ;




/* ======================================================================= */
/* ....................................................................... */
Controller::Controller( Operator controller_operator ):
    m_operator              ( controller_operator ),
    m_state_index           ( 0 ),
    m_accumulator           ( 0 ),
    m_num_sensors           ( 0 )
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
Controller::Controller(const Controller& other, const osg::CopyOp& copyop):
    osg::Object             ( other, copyop ),
    m_operator              ( other.m_operator ),
    m_state_index           ( other.m_state_index ),
    m_accumulator           ( other.m_accumulator ),
    m_num_sensors           ( other.m_num_sensors ),
    m_actuator_list         ( other.m_actuator_list )
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
Controller::~Controller(void)
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
void
Controller::operator()( osgODE::ODEObject* object, Game* game )
{
    PS_ASSERT1( m_operator != USER_DEFINED ) ;



    if( ! getStateBit( game->getState() ) ) {

        notifyActuators( game->getActuatorList(), false ) ;


    } else {


        bool    active = pulse( object, game ) ;

        notifyActuators( game->getActuatorList(), active ) ;
    }
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
bool
Controller::pulse( osgODE::ODEObject* object, Game* game )
{
    PS_ASSERT1( m_operator != USER_DEFINED ) ;

    (void) object ;
    (void) game ;


    bool    active = false ;


    switch( m_operator )
    {
        case USER_DEFINED:
            PS_WARN("ooGame::Controller(%p): base class method invokation on USER_DEFINED operator", this) ;
        break ;


        case AND:
            active = (m_accumulator == m_num_sensors)  &&  (m_num_sensors != 0) ;
        break ;


        case OR:
            active = m_accumulator != 0 ;
        break ;


        case XOR:
            active = m_accumulator == 1 ;
        break ;


        case NAND:
            active = (m_accumulator != m_num_sensors) || (m_num_sensors == 0) ;
        break ;


        case NOR:
            active = (m_accumulator == 0)  ||  (m_num_sensors == 0) ;
        break ;


        case XNOR:
            active = m_accumulator != 1 ;
        break ;


        default:
            PS_BREAKPOINT() ;
        break ;
    }


    return active ;
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
void
Controller::accumulate( bool value )
{
    if( value ) {
        ++m_accumulator ;
    }

    ++m_num_sensors ;
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
void
Controller::notifyActuators( ActuatorList& actuators, bool value )
{
    std::vector<unsigned int>::const_iterator   itr = m_actuator_list.begin() ;
    std::vector<unsigned int>::const_iterator   itr_end = m_actuator_list.end() ;

    while( itr != itr_end ) {

        actuators[ *itr++ ]->accumulate( value ) ;

    }
}
/* ....................................................................... */
/* ======================================================================= */
